<?php
/**
 * Created by PhpStorm.
 * User: 轩
 * Date: 2017/9/3
 * Time: 18:08
 */
namespace app\home\controller\Cloud;
use think\Config;
use think\Session;
use think\Controller;
use think\Db;
use app\home\service\AccountService;
use app\home\controller\Common;
use app\common\Qiniu\PlaySDK;

class Banner extends Controller
{
    private $MINI_ID;
    private $WX_id;
    private $WX_secret;

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $A = new AccountService();
        $this->MINI_ID = Session::get('login_miniID');
        $this->WX_id = $A->getminiAppsData('appid', $this->MINI_ID);
        $this->WX_secret = $A->getminiAppsData('secret', $this->MINI_ID);
    }

    public function index()
    {
        /* +++++++ 页面配置 +++++++ */
        $CONFIG = [
            'title' => '产品图库',
            'css' => [
                'companys/css/webuploader_imgs.css',
                'home/css/plugins/webuploader/webuploader.css'
            ],
            'js' => [
                'home/js/plugins/webuploader/webuploader.js'
            ],
            'path'=>[
                '首页管理'
            ],

        ];
        $this->assign('CONFIG', $CONFIG);
        /* +++++++ 数据输出 +++++++ */
        $common = new Common();
        $upload_Token = $common->getQiniuUploadToken()['data']['token'];
        $read = Db::table('temp_cd_images')->where(['miniapps_id'=>$this->MINI_ID,'type'=>3])->select();
        $DATA = [
            'token' => $upload_Token,
            'images' => $read ? $read : []
        ];
        $this->assign('DATA', $DATA);
        return $this->fetch('_Tpl/cloud/banner');
    }

    public function add_banner_images()     //添加banner图
    {
        if (!isset($_POST['key'])) {
            return ['info' => 0];
        }
        $num = Db::table('temp_cd_images')->where(['miniapps_id'=>$this->MINI_ID,'type'=>3])->count();
        if($num > 0){
            return ['info' => 2];
        }
        $data = array(
            'url'=>$_POST['key'],
            'type'=> 3,
            'miniapps_id'=> $this->MINI_ID,
            'add_time'=>date('y-m-d H:i:s',time()),
        );
        $res= Db::table('temp_cd_images')->insert($data);
        if ($res) {
            return ['info' => 1];
        }
        return ['info' => 0];
    }

    public function del_banner_images()      //删除banner图
    {
        if (!isset($_POST['key'])) {
            return ['info' => 0];
        }
//        $qiniu = new PlaySDK(Config::get('qiniu.appid'), Config::get('qiniu.appsecret'));
//        $qiniu->delFileByKey('suyongcloud', $_POST['key']);
        $res = Db::table('temp_cd_images')->where(['miniapps_id'=>$this->MINI_ID,'url'=>$_POST['key'],'type'=>3])->delete();

        $key = substr($_POST['key'], 22);
        $QINIU = new PlaySDK(Config::get('qiniu.appid'), Config::get('qiniu.appsecret'));
        $QINIU->delFileByKey(Config::get('qiniu.bucket'), $key);

        if ($res) {
            return ['info' => 1];
        }
        return ['info' => 0];
    }
}